package cn.boyu.controller;


import cn.boyu.entity.AssetsAmmeter;
import cn.boyu.entity.Customer;
import cn.boyu.entity.SysUser;
import cn.boyu.query.AmmeterQuery;
import cn.boyu.result.AjaxResult;
import cn.boyu.service.IAssetsAmmeterService;
import cn.boyu.service.ISysUserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yn
 * @since 2022-11-22
 */
@RestController
@CrossOrigin
@RequestMapping("/assetsAmmeter")
public class AssetsAmmeterController {

    @Autowired
    private IAssetsAmmeterService assetsAmmeterService;

    @Autowired
    private ISysUserService sysUserService;

    //显示列表  /  高级查询
    @GetMapping("/query")
    public AjaxResult query(AmmeterQuery qo,HttpServletRequest request) {
        System.err.println("token:::"+request.getHeader("token"));
        IPage<AssetsAmmeter> page = assetsAmmeterService.queryPage(qo);
        return AjaxResult.success(page);
    }

    //修改
    @PostMapping("/update")
    public AjaxResult update(AssetsAmmeter assetsAmmeter) {
        if (assetsAmmeter.getId() != null) {
            assetsAmmeterService.saveOrUpdate(assetsAmmeter);
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error();
        }
    }

    //批量添加电表
    @PostMapping("/save")
    public AjaxResult save(Long firstId, Long lastId,
                           AssetsAmmeter assetsAmmeter, HttpServletRequest request) {
        //获取当前登录用户
        String token = request.getHeader("token");
        SysUser user = sysUserService.getUserByToken(token);

        //添加操作人 和创建时间
        assetsAmmeter.setCreateTime(new Date());
        assetsAmmeter.setUserId(user.getUserId());

        assetsAmmeterService.saveAssets(firstId, lastId, assetsAmmeter);
        return AjaxResult.success("添加成功");
    }

//    //批量删除电表  (逻辑删除  只改变删除标识）
//    @PostMapping("/delete")
//    public AjaxResult delete(Long ids[]) {
//        assetsAmmeterService.batchDelete(ids);
//        return AjaxResult.success("删除成功");
//    }

    //批量删除电表  (逻辑删除  只改变删除标识）
    @PostMapping("/delete")
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        Long[] arr = new Long[split.length];
        for (int i = 0; i < split.length; i++) {
            arr[i] = Long.parseLong(split[i]);
        }
        assetsAmmeterService.batchDelete(arr);
        return AjaxResult.success("删除成功");
    }

    /*首页 统计信息接口*/
    @GetMapping("/count")
    public AjaxResult count(){
        int count = assetsAmmeterService.count(new QueryWrapper<AssetsAmmeter>().eq("del_flg",0));
        return AjaxResult.success(count);
    }

}

